from PyQt5.Qt import *
from qfluentwidgets import FlowLayout
from qfluentwidgets.window.stacked_widget import StackedWidget

from components.card import AppCard
from .word_view import WordToPdfFrame, DocAndDocxFrame


class WordHomeFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.layout = FlowLayout(self, needAni=True)

        self.to_pdf_card = AppCard(QIcon('img/pdf.svg'), 'To PDF', 'Word 批量转成 PDF')
        self.docx_and_doc_card = AppCard(QIcon('img/word.svg'), 'Doc & Docx', 'doc格式 & docx格式 批量互转')
        self.word_append = AppCard(QIcon('img/word3.svg'), 'Word Merge', 'Word 批量合并')
        self.layout.addWidget(self.to_pdf_card)
        self.layout.addWidget(self.docx_and_doc_card)
        self.layout.addWidget(self.word_append)


class WordInterface(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('WordInterface')

        self.stackWidget = StackedWidget()

        self.initLayout()
        self.initStackedWidget()
        self.initButtonEvent()

    def initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 10, 10, 10)
        self.hBox.setSpacing(0)
        self.hBox.addWidget(self.stackWidget)

    def initStackedWidget(self):
        self.homeFrame = WordHomeFrame()
        self.wordToPdfFrame = WordToPdfFrame()
        self.docAndDocxFrame = DocAndDocxFrame()

        self.stackWidget.addWidget(self.homeFrame)
        self.stackWidget.addWidget(self.wordToPdfFrame)
        self.stackWidget.addWidget(self.docAndDocxFrame)

        self.stackWidget.setCurrentIndex(0)

    def initButtonEvent(self):
        self.homeFrame.to_pdf_card.clicked.connect(lambda: self.switchTo(self.wordToPdfFrame))
        self.homeFrame.docx_and_doc_card.clicked.connect(lambda: self.switchTo(self.docAndDocxFrame))
        self.wordToPdfFrame.returned.connect(lambda: self.switchTo(self.homeFrame))
        self.docAndDocxFrame.returned.connect(lambda: self.switchTo(self.homeFrame))

    def switchTo(self, widget):
        self.stackWidget.setCurrentWidget(widget)


if __name__ == '__main__':
    import sys

    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

    app = QApplication(sys.argv)
    win = WordInterface()
    win.show()
    app.exec_()
